package amartin47;

import java.io.*;
import java.net.*;
import java.util.*;

public class LyricsServer extends Thread {

   private ServerSocket dateServer;
   public boolean stopServer = false;
   private PageChooser chooser;

   public LyricsServer(PageChooser chooser){
     try {
		 dateServer = new ServerSocket(3000);
		 this.chooser = chooser;
	     this.start();
	} catch (IOException e) {
		e.printStackTrace();
		System.out.println("Failed to Open Socket");
	}
   } 
   
   public void close()
   {
	   try {
		dateServer.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
   }

   public void run() {
     while(!stopServer) {
       try {
        //System.out.println("Waiting for connections.");
        Socket client = dateServer.accept();
        //System.out.println("Accepted a connection from: " + client.getInetAddress());
        synchronized(chooser)
        {
        	String lyrics = "";
        	if(chooser.getSelectedSlide() != null)
        		lyrics = chooser.getSelectedSlide().lyrics;
        	Connect c = new Connect(client, lyrics);
        }
       } catch(Exception e) {}
     }
     System.out.println("Stop Server");
   }
}
